/*
 * OperatorListenerHandler.java
 *
 * Created on June 17, 2007, 2:48 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jhub.conn;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import jhub.HubSession;
import jhub.UserSession;
import jhub.conn.exception.InsufficientPrivilegeException;
import jhub.management.Ban;
import jhub.text.KickMessage;
import jhub.text.OpForceMoveMessage;

/**
 *
 * @author dbotelho
 */
public class OperatorListenerHandler extends RegisteredListenerHandler {
    
    /**
     * Creates a new instance of OperatorListenerHandler
     */
    public OperatorListenerHandler(HubSession hubSession,UserSession user) {
        super(hubSession,user);
    }
    
    protected void handlerKick(KickMessage kickMessage) throws InsufficientPrivilegeException {
        try {
            Calendar banTill = new GregorianCalendar();
            banTill.add(Calendar.MINUTE,this.hubSession.getHub().getDefaultKickTime());
            
            Ban ban = Ban.nickInstance(kickMessage.getNick(),"kick",new Date(),banTill.getTime());
            ban.setIp(this.hubSession.getUserSessionList().getUser(kickMessage.getNick()).getIp());
            
            this.hubSession.getHub().getUserManager().kick(ban);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        this.hubSession.getUserSessionList().removeUser(this.hubSession.getUserSessionList().getUser(kickMessage.getNick()));
    }
    
    protected void handlerOpForceMove(OpForceMoveMessage opForceMoveMessage) throws InsufficientPrivilegeException, UnsupportedEncodingException, IOException, ParseException {
        this.hubSession.getUserSessionList().getUser(opForceMoveMessage.getVictimNick()).writeForceMove(opForceMoveMessage);
        this.hubSession.getUserSessionList().removeUser(this.hubSession.getUserSessionList().getUser(opForceMoveMessage.getVictimNick()));
    }
    
}
